// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ChildViewModel.cs" company="XamlmNinja">
//   2010
// </copyright>
// <summary>
//   The child view model.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ServiceStyleRatherThanMessengerStyle.ViewModel
{
    using System;
    using System.Threading;

    using GalaSoft.MvvmLight.Command;
    using GalaSoft.MvvmLight.Messaging;

    using ServiceStyleRatherThanMessengerStyle.Model;
    using ServiceStyleRatherThanMessengerStyle.Services;
    using ServiceStyleRatherThanMessengerStyle.ViewModel.Messages;
    using WP7Contrib.Logging;
    using WP7Contrib.Services.Navigation;
    using WP7Contrib.Services.Storage;

    /// <summary>
    /// The child view model.
    /// </summary>
    public class ChildViewModel : ViewModelBaseWp7
    {
        #region Constants and Fields

        /// <summary>
        /// The favourites service.
        /// </summary>
        private readonly IFavouritesService favouritesService;

        /// <summary>
        /// The currently selected person.
        /// </summary>
        private Person currentlySelectedPerson;

        /// <summary>
        /// The select add to favourites command.
        /// </summary>
        private RelayCommand selectAddToFavouritesCommand;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ChildViewModel"/> class.
        /// </summary>
        /// <param name="navigationService">
        /// The navigation service.
        /// </param>
        /// <param name="messenger">
        /// The messenger.
        /// </param>
        /// <param name="log">
        /// The log.
        /// </param>
        /// <param name="favouritesService">
        /// The favourites Service.
        /// </param>
        public ChildViewModel(
            INavigationService navigationService, IMessenger messenger, ILog log, IFavouritesService favouritesService)
            : base(navigationService, messenger, log)
        {
            this.favouritesService = favouritesService;

            ThreadPool.QueueUserWorkItem(
                state => this.MessengerInstance.Register<SelectedPersonMessage>(this, this.OnReceiveMessage));
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets CurrentlySelectedCurrentlySelectedPerson.
        /// </summary>
        public Person CurrentlySelectedPerson
        {
            get
            {
                return this.currentlySelectedPerson;
            }

            private set
            {
                this.SetPropertyAndNotify(ref this.currentlySelectedPerson, value, "CurrentlySelectedPerson");
            }
        }

        // <summary>
        /// <summary>
        /// Gets SelectAddToFavouritesCommand.
        /// </summary>
        public RelayCommand SelectAddToFavouritesCommand
        {
            get
            {
                return this.selectAddToFavouritesCommand ??
                       (this.selectAddToFavouritesCommand = new RelayCommand(this.ExecuteAddToFavouritesCommand));
            }

            private set
            {
                this.selectAddToFavouritesCommand = value;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// The is being activated.
        /// </summary>
        /// <param name="storage">
        /// The storage.
        /// </param>
        protected override void IsBeingActivated(IStorage storage)
        {
        }

        /// <summary>
        /// The is being deactivated.
        /// </summary>
        /// <param name="storage">
        /// The storage.
        /// </param>
        protected override void IsBeingDeactivated(IStorage storage)
        {
        }

        /// <summary>
        /// The execute add to favourites command.
        /// </summary>
        private void ExecuteAddToFavouritesCommand()
        {
            this.favouritesService.Add(this.CurrentlySelectedPerson);

            // need to add a messenger here so that we can show that if we wanted to use the serivce and the messenger together than we can do
            // this is the final piece to the puzzle
            this.NavigationService.Navigate(new Uri("/View/FavouritesPage.xaml", UriKind.Relative));
        }

        /// <summary>
        /// The on receive message.
        /// </summary>
        /// <param name="obj">
        /// The obj.
        /// </param>
        private void OnReceiveMessage(SelectedPersonMessage obj)
        {
            if (obj == null)
            {
                return;
            }

            if (obj.Person == null)
            {
                return;
            }

            this.CurrentlySelectedPerson = obj.Person;
        }

        #endregion
    }
}